package com.anrongtec.zcpt.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.NotificationAdapter;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.model.NotifyList;

import java.util.ArrayList;
import java.util.List;

public class NotificationActivity extends BaseActivity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener{

    private ListView lvNotification;
    private ImageView notiState;
    private TextView tv_title;
    private View moreView; //加载更多页面
    private TextView timeTv;
    private NotificationAdapter adapter;
    private List<NotifyList> lists = new ArrayList<>();


    private int pageSize = 1;//总页数
    private int pageNo = 1;//当前页
    private int lastItem;
    private int count;
    //声明Handler
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    getNotificationInfo();  //加载更多数据，这里可以使用异步加载
                    break;
                default:
                    break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_notification);
        initTopBar(this,true,"通知通告","");
        getNotificationInfo();

        moreView = LayoutInflater.from(this).inflate(R.layout.progressbar_loar_data, null);
        lvNotification = (ListView) findViewById(R.id.lv_notification);
        lvNotification.addFooterView(moreView); //添加底部view，一定要在setAdapter之前添加，否则会报错。
        adapter = new NotificationAdapter(lists, NotificationActivity.this);
        lvNotification.setAdapter(adapter);
        lvNotification.setOnItemClickListener(NotificationActivity.this);
        lvNotification.setOnScrollListener(NotificationActivity.this);
    }

    private void getNotificationInfo() {
//        NotificationReq req = new NotificationReq("10", String.valueOf(pageNo));
//        final MobKey mobKey = new MobKey();
//        String value = req.toJson(mobKey);
//        OkHttpClientManager.postAsyn(value, new JsonResultCallback<NotificationRes>() {
//            @Override
//            public void onResponse(NotificationRes response) {
//                if (response.isSuccess()) {
//                    List<NotifyList> notifyList = response.getNotifyList();
//                    lists.addAll(notifyList);
////                    if (notifyList.size() < 5&&notifyList.size()>= 0){
////                        pageNo--;
////                     }
//                    pageSize = Integer.parseInt(response.getPageSize());
//                    pageNo++;
//                    count = lists.size();
//
////                    if (notifyList.size() < 10) {
//                    moreView.setVisibility(View.GONE);
////                    }
//                    adapter.notifyDataSetChanged();
//
//                } else {
//                    toToast(response.getRetMsg());
//                }
//            }
//
//        });

        for (int i = 0; i < 10; i++) {
            NotifyList nf=new NotifyList("公安网","2017-2-14","中国警察网是中国最权威的警务资讯综合门户网站,设有新闻、视频、大要案、经济犯罪预警、出入境信息.....","人民公安报 - 中国警察网");
            lists.add(nf);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, NotificationContentActivity.class);
        NotifyList notifyList = lists.get(position);
        intent.putExtra("title",notifyList.getTitle());
        intent.putExtra("time",notifyList.getCreateDate());
        intent.putExtra("content",notifyList.getContent());
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //下拉到空闲是，且最后一个item的数等于数据的总数时，进行更新
        if (pageNo < pageSize && lastItem == count&& scrollState == this.SCROLL_STATE_IDLE) {
            moreView.setVisibility(View.VISIBLE);
            mHandler.sendEmptyMessage(0);
        }
        if (pageNo >= pageSize ){

            showToastCustom("没有更多数据了", ApiConfig.WARNING);
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        lastItem = firstVisibleItem + visibleItemCount - 1;  //减1是因为上面加了个addFooterView
    }

}
